package com.ns002.yigou.quote.common.app;

import com.ns002.yigou.quote.domain.OpenCloseDate;
import com.opslab.util.DateUtil;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author xu
 * @Date 2017/7/26 0026 下午 9:58
 * 行情时间（开盘休市）
 */
@Component
public class QuoteDate {

    private static final SimpleDateFormat dayFullFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat openCloseDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static Date getDayFull(Date date) {
        try {
            return dayFullFormat.parse(dayFullFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取交易日， 如果是周六周日就返回周五
     * @param alias
     * @return
     * @throws ParseException
     */
    public synchronized static OpenCloseDate getPreviousOpenCloseDateByAlias(String alias) throws ParseException {
        //设置开盘休市时间  找到有数据为止
        Date quoteDate = new Date();
        while (true) {
            OpenCloseDate openCloseDate = getOpenCloseDateByAlias(alias, quoteDate);
            if (openCloseDate == null || openCloseDate.getOpenDate() == null) {
                quoteDate = new Date(quoteDate.getTime() - (60000 * 60 * 24));
                continue;
            }
            return openCloseDate;

        }
    }

    static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");

    public synchronized static OpenCloseDate getOpenCloseDateByAlias(String alias, Date date) throws ParseException {

//        开盘休市时间 14点时：openDate是正确的但是closeDate会少一天，1点时：closeDate是正确的，openDate会多一天
//        夏令时：石化宝、白银周一开盘早上8点收盘凌晨4点，其他交易日开盘早上6点收盘凌晨4点
//                镍开盘早上9点收盘凌晨2点
//        冬令时：石化宝、白银周一开盘早上8点收盘凌晨4点，其他交易日开盘早上7点收盘凌晨4点
//                镍开盘早上10点收盘凌晨3点

        //现在的时间
        String currentDateStr = yyyyMMdd.format(date);

        String openTime = null;
        String closeTime = null;
        //当天的时间整
        Date dayFull = yyyyMMdd.parse(currentDateStr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week = cal.get(Calendar.DAY_OF_WEEK);

        switch (alias) {
            case "XAG1":
            case "OIL":
            case "JOIL":
            case "JCU":
            case "JXAG":
                if (week == 2) {
                    openTime = "08:00";
                } else {
                    openTime = "06:00";
                }
                closeTime = "04:00";
                break;
            case "HGNI":
                openTime = "09:00";
                closeTime = "02:00";
                break;
            case "URP":
                openTime = "06:00";
                closeTime = "04:00";
                break;
            case "S":
            case "W":
                openTime = "08:00";
                closeTime = "02:20";
                break;
            case "KC":
                openTime = "16:15";
                closeTime = "01:30";
                break;

            case "EUR":
            case "JPY":
            case "GBP":
            case "AUD":
            case "NZD":
            case "DINIW":
                if (week == 2) {
                    openTime = "08:00";
                } else {
                    openTime = "06:00";
                }
                closeTime = "04:00";
                break;

            default:
                openTime = "10:00";
                closeTime = "03:00";
                break;
        }
        Date openDate = openCloseDateFormat.parse(currentDateStr + " " + openTime);
        Date closeDate = openCloseDateFormat.parse(currentDateStr + " " + closeTime);
        long simulate = openDate.getTime() - dayFull.getTime();
        cal.setTime(new Date(date.getTime() - simulate));
        week = cal.get(Calendar.DAY_OF_WEEK);

        if (week == 1 || week == 7) {
            OpenCloseDate openCloseDate = new OpenCloseDate();
            openCloseDate.setCloseDate(null);
            openCloseDate.setOpenDate(null);
            return openCloseDate;
        }

        if (openDate.getTime() > date.getTime()) {
            //不可能存在开盘时间大于现在时间，减去一天
            openDate = new Date(openDate.getTime() - 60000 * 60 * 24);
        } else {
            //正常情况休市在明天
            closeDate = new Date(closeDate.getTime() + 60000 * 60 * 24);
        }
        OpenCloseDate openCloseDate = new OpenCloseDate();
        openCloseDate.setOpenDate(openDate);
        openCloseDate.setCloseDate(closeDate);

        return openCloseDate;
    }

}
